﻿using Microsoft.Live;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Intex.Tools;
namespace WPFExcelReport
{
    public partial class SDTDBSYNData
    {
        public Queue<KeyValuePair<string, string>> jobQ = null;
        LiveConnectClient client;
        int running = 0;
        ArrayList folders = new ArrayList();
        public LiveConnectClient LiveClient
        {
            get
            {
                return client;
            }
            set
            {
                client = value;
                foreach (string folder in folders)
                {
                    client.GetFolderID(folder);
                }
            }
        }
        public void ZipFolder(string rootx = @"C:\IntexReport\MINTEX")
        {
            string zipfile = "mintexdb" + System.DateTime.Now.ToFileTime().ToString() + ".zip";
            GZip.Compress(rootx + "\\PhoneDB", rootx + "\\ZZContent\\", zipfile);
            System.IO.File.Move(rootx + "\\ZZContent\\" + zipfile, rootx + "\\ZZContent\\" + zipfile + ".ITX");
        }
        public async void UploadZip(string rootx = @"C:\IntexReport\MINTEX")
        {
            jobQ = new Queue<KeyValuePair<string, string>>();
            try
            {
                string[] phondb = System.IO.Directory.GetDirectories(rootx);
                foreach (string drs in phondb)
                {
                    if (!drs.EndsWith("ZZContent")) continue;
                    string[] files = System.IO.Directory.GetFiles(drs);
                    string folder = drs.Replace(rootx + "\\", "");
                    await LiveClient.GetFolderID(folder);
                    foreach (string file in files)
                    {
                        if (file.EndsWith(".ITX"))
                        {
                            jobQ.Enqueue(new KeyValuePair<string, string>(file, folder));
                        }
                    }
                }
                ExecuteJob(true);
            }
            catch (Exception ex)
            {
            }
        }
        public async void UploadPictures(string rootx = @"C:\IntexReport\MINTEX")
        {
            jobQ = new Queue<KeyValuePair<string, string>>();
            try
            {
                string[] phondb = System.IO.Directory.GetDirectories(rootx);
                foreach (string drs in phondb)
                {
                    if (!drs.EndsWith("Pictures")) continue;
                    string[] files = System.IO.Directory.GetFiles(drs);
                    string folder = drs.Replace(rootx + "\\", "");
                    await LiveClient.GetFolderID(folder);
                    foreach (string file in files)
                    {
                        jobQ.Enqueue(new KeyValuePair<string, string>(file, folder));
                    }
                }
               ExecuteJob(true);
            }
            catch (Exception ex)
            {
            }
        }
        public async void UpdateSkydriveFileList(string rootx = @"C:\IntexReport\MINTEX")
        {
           await LiveClient.RefreshFolderList(rootx + "\\PhoneDB");
           await LiveClient.RefreshFileList(rootx + "\\PhoneDB");
            jobQ = new Queue<KeyValuePair<string, string>>();
            try
            {
                string[] files = System.IO.Directory.GetFiles(rootx + "\\PhoneDB");
                string folder = "PhoneDB";
                foreach (string file in files)
                {
                    if (file.EndsWith(".ITX"))
                    {
                        jobQ.Enqueue(new KeyValuePair<string, string>(file, folder));
                    }
                }
                ExecuteJob(false);
            }
            catch (Exception ex)
            {
            }
        }
        public async void ExecuteJob(bool IsKeep = false)
        {
            LiveOperationResult result;
            try
            {
                while (jobQ.Count > 0)
                {
                    if (running < 50)
                    {
                        if (jobQ.Count == 0)
                            break;
                        KeyValuePair<string, string> kp = jobQ.Dequeue();
                        try
                        {
                            string localfile = kp.Key;
                            string folder = kp.Value;
                            string destpath = LiveClient.CheckFolderID(folder);
                            string filepath = localfile;
                            MemoryStream memStream = new MemoryStream();
                            FileInfo file = new FileInfo(filepath);
                            string filename = file.Name.Replace(".ITX", "");
                            using (FileStream fileStream = File.OpenRead(filepath))
                            {
                                memStream.SetLength(fileStream.Length);
                                fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
                            }
                            running++;
                            result = await LiveClient.UploadAsync(destpath, filename, memStream, OverwriteOption.Overwrite);
                            running--;
                            if (result.Error == null)
                            {
                                if (!IsKeep)
                                {
                                    System.IO.File.Delete(filepath);
                                    System.IO.File.Delete(filepath + ".ITX");
                                    //System.IO.File.Move(filepath, filepath.Replace(".ITX", ""));
                                }
                                else
                                {
                                    System.IO.File.Delete(filepath);
                                    System.IO.File.Move(filepath, filepath.Replace(".ITX", ""));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
